package com.innovation.chengzhangriji.ui;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.google.gson.JsonElement;
import com.innovation.chengzhangriji.Config;
import com.innovation.chengzhangriji.R;
import com.innovation.chengzhangriji.http.Client;
import com.innovation.chengzhangriji.http.ResponseHandler;
import com.innovation.chengzhangriji.ui.animation.AnimationConfig;
import com.innovation.chengzhangriji.ui.widget.CuteButton;
import com.innovation.chengzhangriji.ui.widget.CuteEditGroup;
import com.jiuan.oa.android.library.util.MobileNumberUtil;
import com.umeng.analytics.MobclickAgent;

public class SignUp1Fragment extends BaseFragment implements View.OnClickListener {

    public interface SignUpGetCodeListener {

        /**
         * 注册获取验证码成功
         */
        void signUpGetCodeSuccess(String phone);
    }

    private SignUpGetCodeListener mSignUpGetCodeListener;

    private CuteEditGroup mCegNumber;

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 1 && resultCode == Activity.RESULT_OK) {
            getActivity().finish();
        }
    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        try {
            mSignUpGetCodeListener = (SignUpGetCodeListener) activity;
        } catch (ClassCastException e) {
            throw new ClassCastException(activity.toString()
                    + " must implement SignUpGetCodeListener");
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_signup_1, container, false);
        mCegNumber = (CuteEditGroup) view.findViewById(R.id.edit_text_number);
        CuteButton cbNext = (CuteButton) view.findViewById(R.id.btn_next);
        cbNext.setOnClickListener(this);
        return view;
    }

    @Override
    protected void onPageStart() {
        MobclickAgent.onPageStart(Config.UMENG_PAGE_SIGN_1);
    }

    @Override
    protected void onPageEnd() {
        MobclickAgent.onPageEnd(Config.UMENG_PAGE_SIGN_1);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_next:
                String number = mCegNumber.getContent();
                if (checkPhone(number)) {
                    requestSignUpGetCode(number);
                }
                break;
        }
    }

    public void setSignUpGetCodeListener(SignUpGetCodeListener signUpGetCodeListener) {
        mSignUpGetCodeListener = signUpGetCodeListener;
    }

    private boolean checkPhone(String content) {
        if (!MobileNumberUtil.isMobile(content)) {
            AnimationConfig.shake(getActivity(), mCegNumber);
            tipShow(R.string.tip_error_number);
            return false;
        }
        return true;
    }

    private void requestSignUpGetCode(final String number) {
        Client.requestSignUpGetCode(getActivity(), number, new ResponseHandler() {

            @Override
            public void onInnovationStart() {
                showProgressDialog(null, null);
            }

            @Override
            public void onInnovationSuccess(JsonElement value) {
                if (mSignUpGetCodeListener == null) {
                    return;
                }
                dismissProgressDialog();
                mSignUpGetCodeListener.signUpGetCodeSuccess(number);
            }

            @Override
            public void onChZhRJFailure(String value) {
                dismissProgressDialog();
                if (value.equals("手机号被占用")) {
                    Intent intent = new Intent(getActivity(), ErrorDialogActivity.class);
                    startActivityForResult(intent, 1);
                } else {
                    tipShow(value);
                }
            }

        }, networkUnavailableSampleImpl());
    }
}
